import { FacilityType } from "../info/GameEnum";
import GameManager from "../manager/GameManager";
import PlayerManager from "../manager/PlayerManager";
import Player from "../player/Player";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UiMakeHome extends cc.Component {

    controlPlayer: Player = null;
    animation: cc.Animation;
    play: boolean;
    button: cc.Button;
    start() {
        this.controlPlayer = PlayerManager.instance.controllerPlayer;
        this.controlPlayer.node.on(cc.Node.EventType.POSITION_CHANGED, this.checkInMakeHome, this);
        this.controlPlayer.node.on(cc.Node.EventType.CHILD_REMOVED, this.checkExist, this);
        this.animation = this.getComponent(cc.Animation);
        this.button = this.getComponent(cc.Button);
    }

    checkExist(node: cc.Node) {
        if (node.name == "showRole") {
            this.node.destroy();
        }
    }

    /**
     * 如果在安家点内，播放动画
     */
    checkInMakeHome() {
        if (GameManager.instance.getFacilityByPosition(this.controlPlayer.node.position)?.facilityType == FacilityType.安家点) {
            if (!this.play) {
                this.play = true;
                this.animation.play();
                this.button.interactable = true;
            }
        } else {
            this.play = false;
            this.animation.stop();
            this.button.interactable = false;
        }
    }


    protected onDestroy(): void {
        this.controlPlayer.node?.targetOff(this);
    }

    // update (dt) {}
}
